<?php
namespace Index\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function LoginAction(){  
    	if(IS_POST){
            // var_dump(I('POST.'));
            // exit();
            $username = I('POST.user_name');
            $password = I('POST.password');
    		$data = M();
    		$sql = "select * from user where username = '{$username}'";
    		$res = $data->query($sql);
    		// var_dump($res);
            // exit();
    		if(empty($res)){
    			$this->error("邮箱或密码不正确1",U('index/login/login'),2);
    		}
    		$res = current($res);
    		// var_dump(md5($password));
    		// var_dump($res['password']);
    		// exit();
    		if($res['password']!=md5($password)){
    			$this->error("邮箱或密码不正确2",U('index/login/login'),2);
    		}else{
    			$_SESSION['username'] = $username;
                $_SESSION['id'] = $res['id'];
    			$_SESSION['gid'] = $res['gid'];
    			$_SESSION['face'] = $res['head_img'];
                $login_time=date("Y-m-d H:i:s");
                //将用户登录时间插入到数据库中
                $sql = "update user set login_time='$login_time' where username ='$username'";
                $data_m = M();
                $res = $data_m->execute($sql);
                // var_dump($res);
                // exit();
    			$this->success('登录成功',U('index/Find/Index'),2);
    			return ;
    		}
    	}else{

              $this->display();
        }      
    }
    //退出登录
    public function LoginoutAction(){
        session_unset();
        session_destroy();
        $this->success('登出成功',U('index/setting/basing'),2);
    }


}